<?php
/**
 * 在代理中编写具体逻辑，对外仅暴露简单接口
 */

namespace App\DesignPattern\Proxy;

use App\DesignPattern\Proxy\IUserProxy;
use App\DesignPattern\Factory\OrmFactory;


class UserProxy implements IUserProxy
{
    /**
     * @var \FizeORM\Mysql\OrmMysqlPdo
     */
    private $orm = null;

    public function __construct()
    {
        $this->orm = OrmFactory::createOrm();
    }

    public function setUserName($id, $name)
    {
        $result = $this->orm->table('user')->where("`id`={$id}")->update(['name' => $name]);
        return $result === false ? false : true;
    }

    public function getUserName($id)
    {
        $user = $this->orm->table('user')->where("`id`={$id}")->find();
        if(empty($user))
        {
            return false;
        }
        return $user['name'];
    }
}